package zhihui.mobile.manager.shop;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import zhihui.mobile.bean.shop.address.ShoppingAddress;
import zhihui.mobile.config.Config;
import zhihui.mobile.interfaces.net.IHttpCallBackListener;
import zhihui.mobile.manager.IManager;
import zhihui.mobile.manager.user.UserManager;
import zhihui.mobile.net.HttpUtils;

/**
 * Created by 张公铭 on 2017/8/28 0028.
 */

public class ShopAddressManager implements IManager
{
    private static ShopAddressManager manager;
    private List<ShoppingAddress> mList = new ArrayList<>();       //当前用户的收货地址信息
    private ShoppingAddress curAddress = new ShoppingAddress();     //当前用户选择的收货地址

    private synchronized static void created()
    {
        if (manager == null)
        {
            manager = new ShopAddressManager();
        }
    }

    public static ShopAddressManager getInstance()
    {
        if (manager == null)
        {
            created();
        }
        return manager;
    }

    public List<ShoppingAddress> getmList()
    {
        return mList;
    }

    public void setmList(List<ShoppingAddress> list)
    {
        if (mList == null)
        {
            mList = new ArrayList<>();
        }
        mList.clear();
        mList.addAll(list);
    }

    public void setCurAddress(ShoppingAddress curAddress)
    {
        this.curAddress = curAddress;
    }

    public ShoppingAddress getCurAddress()
    {
        return curAddress;
    }

    /**
     * 获取默认地址
     */
    public ShoppingAddress getDefaultAddress()
    {
        for (ShoppingAddress address : mList)
        {
            if (address.getIsdefault() == 1)
            {
                return address;
            }
        }
        return null;
    }

    /**
     * 获取用户送货地址信息
     */
    public void getUserAddress(IHttpCallBackListener listener)
    {
        Map<String, String> params = new HashMap<>();
        params.put("service", "org.zhmm.app.api.cart.Cart");
        params.put("method", "findAddressByUserId");

        List<Object> list = new ArrayList<>();
        list.add(UserManager.getInstance().getCurUser().getId());
        HttpUtils.postAes(Config.URL, params, list, listener);
    }

    /**
     * 增加|修改收货地址
     */
    public void addAddress(IHttpCallBackListener listener, ShoppingAddress address)
    {
        Map<String, String> params = new HashMap<>();
        params.put("service", "org.zhmm.app.api.cart.Cart");
        params.put("method", "addAddress");

        List<Object> list = new ArrayList<>();
        list.add(address.getId() == 0 ? "" : address.getId());
        list.add(UserManager.getInstance().getCurUser().getId());
        list.add(address.getTel());
        list.add(address.getName());
        list.add(address.getProv());
        list.add(address.getProvcode());
        list.add(address.getCity());
        list.add(address.getCitycode());
        list.add(address.getDist());
        list.add(address.getDistcode());
        list.add(address.getDetails());

        HttpUtils.postAes(Config.URL, params, list, listener);
    }

    public void delete(IHttpCallBackListener listener, int id)
    {
        Map<String, String> params = new HashMap<>();
        params.put("service", "org.zhmm.app.api.cart.Cart");
        params.put("method", "delAddress");

        List<Object> list = new ArrayList<>();
        list.add(id);
        HttpUtils.postAes(Config.URL, params, list, listener);
    }

    public void setDefault(IHttpCallBackListener listener, int id)
    {
        Map<String, String> params = new HashMap<>();
        params.put("service", "org.zhmm.app.api.cart.Cart");
        params.put("method", "setDefault");

        List<Object> list = new ArrayList<>();
        list.add(UserManager.getInstance().getCurUser().getId());
        list.add(id);
        HttpUtils.postAes(Config.URL, params, list, listener);
    }

    @Override
    public void clear()
    {

    }
}
